import pygame
from typing import List, Optional, Tuple
import time
import os

class UIManager:
    def __init__(self, width: int, height: int, 
                 normal_font_size: int = 16,
                 small_font_size: int = 14,
                 tiny_font_size: int = 12):
        """初始化UI管理器"""
        pygame.font.init()
        
        # 设置字体大小
        self.normal_font_size = normal_font_size
        self.small_font_size = small_font_size
        self.tiny_font_size = tiny_font_size
        
        # 直接使用项目中的黑体字体
        try:
            font_path = os.path.join('assets', 'fonts', 'simhei.ttf')
            if os.path.exists(font_path):
                self.normal_font = pygame.font.Font(font_path, self.normal_font_size)
                self.small_font = pygame.font.Font(font_path, self.small_font_size)
                self.tiny_font = pygame.font.Font(font_path, self.tiny_font_size)
                self.title_font = pygame.font.Font(font_path, int(self.normal_font_size * 2))
                print(f"成功加载字体: {font_path}")
            else:
                raise FileNotFoundError(f"找不到字体文件: {font_path}")
        except Exception as e:
            print(f"加载字体失败: {e}")
            print("使用系统默认字体")
            self.normal_font = pygame.font.SysFont(None, self.normal_font_size)
            self.small_font = pygame.font.SysFont(None, self.small_font_size)
            self.tiny_font = pygame.font.SysFont(None, self.tiny_font_size)
            self.title_font = pygame.font.SysFont(None, int(self.normal_font_size * 2))
        
        self.width = width
        self.height = height
        self.messages = []  # 用于存储消息
        self.tooltips = []  # 用于存储工具提示
        
    def add_message(self, text: str, color: tuple = (255, 255, 255), duration: float = 2.0):
        """添加一条消息"""
        self.messages.append({
            'text': text,
            'color': color,
            'time': duration
        })
        if len(self.messages) > 10:  # 限制消息数量
            self.messages.pop(0)
    
    def show_tooltip(self, text: str, pos: tuple):
        """显示工具提示"""
        self.tooltips = [(text, pos)]
    
    def update(self):
        """更新UI状态"""
        # 更新消息持续时间
        self.messages = [msg for msg in self.messages if msg['time'] > 0]
        for msg in self.messages:
            msg['time'] -= 1/60  # 假设60FPS
    
    def draw(self, surface):
        """绘制UI元素"""
        # 绘制工具提示
        for text, pos in self.tooltips:
            # 分割多行文本
            lines = text.split('\n')
            max_width = max(self.normal_font.size(line)[0] for line in lines)
            
            # 创建工具提示背景
            height = len(lines) * 25 + 10
            tooltip = pygame.Surface((max_width + 20, height), pygame.SRCALPHA)
            tooltip.fill((0, 0, 0, 200))
            
            # 绘制文本
            for i, line in enumerate(lines):
                text_surface = self.normal_font.render(line, True, (255, 255, 255))
                tooltip.blit(text_surface, (10, 5 + i * 25))
            
            # 确保工具提示不会超出屏幕边界
            x = min(pos[0], self.width - max_width - 20)
            y = min(pos[1], self.height - height)
            
            surface.blit(tooltip, (x, y)) 